/*
 * create:2016年4月8日 上午10:05:15
 * author:wangds  
 */
package com.cxkj.shop.proc;

import javax.servlet.http.HttpSession;

import com.cxkj.shop.biz.ShoppingCartContants;
import com.cxkj.shop.model.shoppingcart.ShoppingCart;
import com.cxkj.shop.model.shoppingcart.ShoppingCartUtils;
import com.cxkj.shop.utils.BusinessUtils;

import net.wangds.common.processor.AbstractProcessorWithResult;

/**
 * .
 * 
 * @author wangds
 *
 */
public class GetShoppingCartFromHttpSessionOrCreateProc extends AbstractProcessorWithResult<ShoppingCart> {

  private HttpSession session;

  /**
   * The getter method of the property session.
   * 
   * @author wangds 2016年4月8日 上午10:06:28.
   * @return the session.
   */
  public HttpSession getSession() {
    return session;
  }

  /**
   * The setter method of the property session.
   * 
   * @param thesession
   *          the session to set
   * @author wangds 2016年4月8日 上午10:06:28.
   */
  public void setSession(HttpSession session) {
    this.session = session;
  }

  /*
   * @see net.wangds.common.processor.AbstractProcessorWithResult#execute()
   * 
   * @author wangds 2016年4月8日 上午10:05:54.
   */
  @Override
  protected ShoppingCart execute() {
    ShoppingCart cart = null;
    if (session != null) {
      synchronized (session) {
        cart = (ShoppingCart) (session.getAttribute(ShoppingCartContants.KEY_SESSION_SHOPPING_CART));
        if (cart == null) {
          cart = ShoppingCartUtils.createShoppingCart(BusinessUtils.getOpenIdSession(session));
          session.setAttribute(ShoppingCartContants.KEY_SESSION_SHOPPING_CART, cart);
        }
      }
    }
    return cart;
  }

}
